package BaseDeDatos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import ClasesBasicas.Sensor;

public class ConsultasGPS {
	
	public static void encenderGPS() {
		
		Connection conn;
		PreparedStatement ps ;
		String url;
		try {
			Class.forName("org.sqlite.JDBC");
			url="jdbc:sqlite:Funcionalidad.db";
			conn = DriverManager.getConnection(url,"","");
			ps = conn.prepareStatement("update Sensor set Estado='ON' where Descripcion = 'Sensor GPS' ;");
			ps.executeUpdate();
			
		} catch (Exception e) {
			System.out.println("Error al encender el sensor GPS");
			e.printStackTrace();
		}
	}
	public static void apagarGPS() {
		
		Connection conn;
		PreparedStatement ps ;
		String url;
		try {
			Class.forName("org.sqlite.JDBC");
			url="jdbc:sqlite:Funcionalidad.db";
			conn = DriverManager.getConnection(url,"","");
			ps = conn.prepareStatement("update Sensor set Estado='OFF' where Descripcion = 'Sensor GPS' ;");
			ps.executeUpdate();
			
		} catch (Exception e) {
			System.out.println("Error al encender el sensor GPS");
			e.printStackTrace();
		}
	}
	public static Sensor getGPS()
	{
		Sensor sensor = new Sensor();
		Connection conn;
		PreparedStatement ps ;
		ResultSet rs;
		String url;
		try {
			Class.forName("org.sqlite.JDBC");
			url="jdbc:sqlite:Funcionalidad.db";
			conn = DriverManager.getConnection(url,"","");
			ps = conn.prepareStatement("Select * from Sensor where Descripcion = 'Sensor GPS' ;");
			rs=ps.executeQuery();
			while(rs.next())
			{
				sensor.setId_sensor(rs.getString("Id_Sensor"));
				sensor.setDescripcion(rs.getString("Descripcion"));
				sensor.setEstado(rs.getString("Estado"));
				
			}
			return sensor;
			
		} catch (Exception e) {
			System.out.println("Error al obtener el sensor GPS");
			return new Sensor();
		}	
	}

}
